package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Andy
 */
@Controller
public class RegisterController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 检查用户名是否存在
     *
     * @param username
     * @return
     */
    @RequestMapping("/checkUserName")
    @ResponseBody
    public boolean checkUserName(String username) {
        return !this.logininfoService.checkUserNameExist(username);
    }

    /**
     * 注册
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public JSONResult register(String username, String password) {
        JSONResult json = new JSONResult();
        try {
            this.logininfoService.register(username, password);
        } catch (RuntimeException e) {
            e.printStackTrace();
            json.setSuccess(false);
            json.setMsg(e.getMessage());
        }
        return json;
    }
}
